package com.actitime.businesslib;

import org.openqa.selenium.support.PageFactory;

import com.actitime.pagefactory.ActiveProjectsAndCustomers;
import com.actitime.pagefactory.CommonPage;
import com.actitime.pagefactory.CreateNewCustomer;
import com.actitime.pagefactory.EditCustomerInformation;
import com.actitime.pagefactory.Login;
import com.actitime.pagefactory.MasterPage;
import com.actitime.pagefactory.OpenTasks;
import com.acttime.GenericLib.Driver;
import com.acttime.GenericLib.WebDriverCommonUtil;

public class ProjectAndCustomerLib {

	WebDriverCommonUtil wLib = new WebDriverCommonUtil();
	Login login = PageFactory.initElements(Driver.driver, Login.class);
	MasterPage masterPage = PageFactory.initElements(Driver.driver, MasterPage.class);
	ActiveProjectsAndCustomers activeProjectsAndCustomer = PageFactory.initElements(Driver.driver, ActiveProjectsAndCustomers.class);
	CommonPage commonPage = PageFactory.initElements(Driver.driver, CommonPage.class);
	CreateNewCustomer createNewCustomer = PageFactory.initElements(Driver.driver, CreateNewCustomer.class);
	OpenTasks openTasks = PageFactory.initElements(Driver.driver, OpenTasks.class);
	EditCustomerInformation editCustomerInformation = PageFactory.initElements(Driver.driver, EditCustomerInformation.class);
	
	public void navigateToProjectAndCustomer()
	{
		// Navigate to tasks tab
		masterPage.getTasksLink().click();
		wLib.waitForPageToLoad();
		// Navigate to Project and Customer
		openTasks.getProjectsAndCustomersLink().click();;
		wLib.waitForPageToLoad();
	}
	
	public void createNewCustomer(String customerName)
	{
		// Click on Create New Customer
		activeProjectsAndCustomer.getCreateNewCustomerBtn().click();;
		wLib.waitForPageToLoad();
		createNewCustomer.getCreateNewCustnameedt().sendKeys(customerName);
		createNewCustomer.getCreateCustomerSubmitBtn().click();
		wLib.waitForPageToLoad();
	}
	
	public boolean verifyCustomer(String customerName)
	{
		//String xpathExpression = "\"//a[Text()='" +customerName+ "']\"";
		String xpathExpression = customerName;
		//String clickableElement = "\"//a[contains(Text(),'Next')]\"";
		String clickableElement = "Next";
		String[] actualCustomer = wLib.clickAndGetRowData(xpathExpression, clickableElement);

		boolean flag = false;
		for(String str:actualCustomer)
		{
			if(customerName.equals(str))
			{
				flag = true;
				break;
			}else
			{
				flag = false;
			}
		}
		return flag;	
	}
	
	public void deleteCustomer(String customerName)
	{
		//Click on customer to be deleted
		wLib.waitForPageToLoad();
		editCustomerInformation.getDeleteCustomerBtn().click();
		wLib.waitForPageToLoad();
		editCustomerInformation.getCustomerDeletedButton().click();
		wLib.waitForPageToLoad();
	}
	
	public void modifyCustomer()
	{
	System.out.println("Modifying customer...");
	editCustomerInformation.getCustomerDescriptionedt().sendKeys("Modified the Customer information");
	editCustomerInformation.getSaveChangesbtn().click();
	System.out.println("Save Customer Information");
	}
}
